<?php
namespace control;
/**
 * 系统参数
 */
class paraClass extends adIndexClass
{
	private const page  = 'para'; # 权限
	private const table = 'para'; # 数据表
	private const title = '系统参数';
	
	/*列表页~查询条件*/
	public function home()
	{
		$html =
			text(array("name" => "id", "title" => "{l.para.home.id}")) .
			text(array("name" => "name", "title" => "{l.para.home.name}")) .
			text(array("name" => "content", "title" => "{l.para.home.content}")) .
			select(array("name" => "edit", "title" => "{l.para.home.edit}", "option" => ["是", "否"]));
		//返回
		return $this->homeFun($this->search($html));
	}
	
	/*系统设置-系统参数~查询结果*/
	public function homeSearch()
	{
		//like查询数组
		$like = array(
			"paName"  => $this->post('name'),//名称
			"paValue" => $this->post('content'),//内容
		);
		//全等查询数组
		$equal = array(
			"paid" => $this->post('id'),//ID号
			"edit" => $this->post('edit'),//可编辑
		);
		//获取查询结果
		$where = " paType = '参数' AND paLang = '" . lang . "'  " . search(array("like" => $like, "=" => $equal));
		$page  = $this->page(["table" => self::table, "where" => $where, "order" => "list"]);
		//获取表头
		// $tr[] = array("", "参数ID", "名称", "参数值", "排序号", "更新时间");
		$tr[] = ["", "{l.para.homeSearch.paid}", "{l.para.homeSearch.name}", "{l.para.homeSearch.value}", "{l.para.homeSearch.list}", "{l.para.homeSearch.time}"];
		//获取记录
		foreach ($page['array'] as $array) {
			if ($this->power(self::page, "edit") and $array['edit'] == "是") {
				$edit = "<img onclick=\"layer('" . self::page . "','detail',{id:'" . $array['paid'] . "'})\" src='" . root . "img/control/edit.svg' title='编辑' class='listIco'>";
			} else {
				$edit = "";
			}
			$name  = zishu($array['paName'], 20);
			$value = zishu($array['paValue'], 60);
			$tr[]  = array($edit, $array['paid'], $name, $value, $array['list'], $array['time']);
		}
		//控件
		$button = "";
		//返回
		$json['html'] = $page['hint'] . $button . "<form name='listForm'>" . space . $this->tableMany(array("tr" => $tr)) . "</form>" . $page['button'];
		return json_encode($json, 320);
	}
	
	/*系统设置-系统参数-编辑*/
	public function detail()
	{
		$para = query(self::table, " paid = '" . $this->post('id') . "' AND paLang = '" . lang . "' " . " AND paType='参数' ");
		$html = $this->layerEdit(array(
			"table" => array(
				array(
					"{l.para.detail.warmTips}" => "{l.para.detail.warmTipsMsg}",
				),
				array(
					"{l.para.detail.paName}" => $para['paName'],
				),
				array(
					must . "{l.para.detail.paValue}" => textarea(array("name" => "paValue", "value" => $para['paValue'])),
				),
				array(
					must . "{l.para.detail.pas}" => password(array("name" => "pas")),
				),
			),
		));
		return $this->layer(array("title" => "{l.para.detail.layerTitle}" . self::title, "height" => 360, "width" => 800, "html" => $html));
	}
	
	/*系统设置-系统参数-新增或更新*/
	public function detailEdit()
	{
		//赋值
		$field = array(
			"paValue" => $this->post('paValue'),   //参数值
			"time"    => date("Y-m-d H:i:s"),      //更新时间
		);
		$para  = query(self::table, " paid = '" . $this->post('id') . "' AND paLang = '" . lang . "' " . " AND paType='参数' ");
		//判断
		if (!$this->power(self::page, "edit")) {
			$json['warn'] = lang('para.detailEdit.powerMsg');
			// $json['warn'] = "你没有编辑的权限";
		} elseif (empty($field['paValue'])) {
			$json['warn'] = lang('para.detailEdit.paValueEmpty');
			// $json['warn'] = "参数值为空";
		} elseif (empty($this->post('pas'))) {
			$json['warn'] = lang('para.detailEdit.pasEmpty');
			// $json['warn'] = "请填写管理员登录密码";
		} elseif (verifyPwd($this->post('pas'), self::$user['password']) !== true) {
			$json['warn'] = lang('para.detailEdit.passwordErr');
			// $json['warn'] = "管理员登录密码输入错误";
		} elseif (empty($this->post('id'))) {
			$json['warn'] = lang('para.detailEdit.paidEmpty');
			// $json['warn'] = "参数ID号为空";
		} elseif (empty($para['paid'])) {
			$json['warn'] = lang('para.detailEdit.paidNone');
			// $json['warn'] = "未找到这条参数";
		} elseif ($para['edit'] == "否") {
			$json['warn'] = lang('para.detailEdit.notedit');
			// $json['warn'] = "此参数不可编辑";
		} else {
			$warn = update(array("table" => self::table, "field" => $field, "where" => " paid = '" . $this->post('id') . "'"));
			if ($warn == "更新成功") {
				//添加日志
				$text = $this->control['name'] . "更新了" . self::title . "：" . $para['paName'] . "-" . $field['paValue'];
				$this->logText(array("type" => self::page, "text" => $text));
				//返回
				$json['warn']     = 2;
				$_SESSION['warn'] = $warn;
			} else {
				$json['warn'] = $warn;
			}
		}
		return json_encode($json, 320);
	}
}
